package com.icss.clothes.service.imp;

import com.icss.clothes.entity.Address;
import com.icss.clothes.entity.User;
import com.icss.clothes.mapper.UserMapper;
import com.icss.clothes.service.IUserService;
import com.icss.clothes.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public  class UserService implements IUserService{
        @Autowired
        private UserMapper userMapper;
        //登录，通过用户名去查找用户信息
        public User loginByUname(String user_name){
                return userMapper.selectUserByUname(user_name);
        }

        @Override
        public boolean register(User user) {
                //从数据库中通过指定的用户名查询用户信息
                User findUser=userMapper.selectUserByUname(user.getUser_name());
                if (findUser!=null){
                        return false;
                }else {
                        userMapper.insertIntoUser(user);
                        return true;
                }
                //向数据库中插入一条指定的用户信息
        }

        //修改用户信息
        @Transactional()
        public boolean updateUser(User user) {
                User findUser = userMapper.selectUserByUname(user.getUser_name());
                if(findUser==null || (findUser.getUser_id()==user.getUser_id() && !findUser.getUser_name().equals(user.getUser_name()))){
                        userMapper.updateUser(user);
                        return true;
                }
                return false;


        }

        @Override
        public List<Address> findAllByUser_id(int user_id) {
                //从数据库中通过指定的用户编号查询得到所有收货地址信息
                return userMapper.selectAllByUser_id(user_id);
        }
}
